package com.leyou.item.web;

import com.leyou.common.enums.ExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
品牌
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value ="page" ,defaultValue ="1" ) Integer page,//当前页
            @RequestParam(value ="rows" ,defaultValue ="5" ) Integer rows,//每页条数
            @RequestParam(value ="sortBy" ,required = false) String sortBy,//排序字段
            @RequestParam(value ="desc" ,defaultValue = "false") Boolean desc,//是否降序
            @RequestParam(value ="key" ,required = false) String key//条件
    ) throws LyException {
        //brandService.queryBrandByPage(page,rows,sortBy,desc,key);

        return ResponseEntity.ok(brandService.queryBrandByPage(page,rows,sortBy,desc,key));
    }

    /**
     * 保存品牌，还要保存品牌的分类（多对多关系添加中间表）
     * @param brand
     * @param cids
     * @return
     * @throws LyException
     */
      @PostMapping
    public  ResponseEntity<Void> saveBrand(Brand brand,@RequestParam("cids") List<Long> cids) throws LyException {

        brandService.saveBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 分类id查询品牌
     * @param cid
     * @return
     */
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> queryByCid(@PathVariable("cid") Long cid){


          return ResponseEntity.ok(brandService.queryBrandByCid(cid));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id){
        return ResponseEntity.ok(brandService.queryById(id));
    }

    @GetMapping("/brands" )
   ResponseEntity<List<Brand>> queryBrandByIds(@RequestParam("ids") List<Long> ids){

        return ResponseEntity.ok(brandService.queryByIds(ids));
    };


}
